<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" v-loading="formLoading">
      <el-form-item label="创建用户" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入创建用户ID" />
      </el-form-item>
      <el-form-item label="存放仓库）" prop="storeId">
        <el-input v-model="formData.storeId" placeholder="请输入存放仓库ID（store表主键）" />
      </el-form-item>
      <el-form-item label="操作类型" prop="operateType">
        <el-select v-model="formData.operateType" placeholder="请选择操作类型：0--入库，1--出库">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="供求信息" prop="infoId">
        <el-input v-model="formData.infoId" placeholder="请输入供求信息ID" />
      </el-form-item>
      <el-form-item label="信息名称" prop="infoName">
        <el-input v-model="formData.infoName" placeholder="请输入信息名称" />
      </el-form-item>
      <el-form-item label="信息编码" prop="infoSn">
        <el-input v-model="formData.infoSn" placeholder="请输入信息编码(无可不填)" />
      </el-form-item>
      <el-form-item label="库存数量" prop="infoNums">
        <el-input v-model="formData.infoNums" placeholder="请输入库存数量" />
      </el-form-item>
      <el-form-item label="信息条码(扫码条码)" prop="infoBn">
        <el-input v-model="formData.infoBn" placeholder="请输入信息条码(扫码条码)" />
      </el-form-item>
      <el-form-item label="信息备注" prop="infoDesc">
        <el-input v-model="formData.infoDesc" placeholder="请输入信息备注" />
      </el-form-item>
      <el-form-item label="出库状态" prop="infoStatus">
        <el-radio-group v-model="formData.infoStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="添加方式" prop="addType">
        <el-select v-model="formData.addType" placeholder="请选择添加方式：0--手动出/入库，1--扫码导出/入，2--批量导出/入">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="扫描方式" prop="scanStatus">
        <el-radio-group v-model="formData.scanStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="关联订单编号" prop="orderId">
        <el-input v-model="formData.orderId" placeholder="请输入关联订单编号(出库)" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { StockInfoApi, StockInfoVO } from '@/api/wine/stockinfo'

/** 出入库信息 表单 */
defineOptions({ name: 'StockInfoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  storeId: undefined,
  operateType: undefined,
  infoId: undefined,
  infoName: undefined,
  infoSn: undefined,
  infoNums: undefined,
  infoBn: undefined,
  infoDesc: undefined,
  infoStatus: undefined,
  addType: undefined,
  scanStatus: undefined,
  orderId: undefined
})
const formRules = reactive({
  userId: [{ required: true, message: '创建用户ID不能为空', trigger: 'blur' }],
  storeId: [{ required: true, message: '存放仓库ID（store表主键）不能为空', trigger: 'blur' }],
  operateType: [{ required: true, message: '操作类型：0--入库，1--出库不能为空', trigger: 'change' }],
  infoId: [{ required: true, message: '供求信息ID不能为空', trigger: 'blur' }],
  infoName: [{ required: true, message: '信息名称不能为空', trigger: 'blur' }],
  infoNums: [{ required: true, message: '库存数量不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await StockInfoApi.getStockInfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as StockInfoVO
    if (formType.value === 'create') {
      await StockInfoApi.createStockInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await StockInfoApi.updateStockInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    storeId: undefined,
    operateType: undefined,
    infoId: undefined,
    infoName: undefined,
    infoSn: undefined,
    infoNums: undefined,
    infoBn: undefined,
    infoDesc: undefined,
    infoStatus: undefined,
    addType: undefined,
    scanStatus: undefined,
    orderId: undefined
  }
  formRef.value?.resetFields()
}
</script>